/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     string val；
 *     TreeNode *left;
 *     TreeNode *right;
 * };
 */
class Solution {
public:
    string expressionTree(TreeNode* root) {
        if(root==nullptr) return "";
        return dfs(root->left)+root->val+dfs(root->right);
    }
    
    string dfs(TreeNode *u)
    {
        if(u==nullptr) return "";
        string ans="";
        if(u->left||u->right) ans="(";
        if(u->left) {ans=ans+dfs(u->left);}
        ans=ans+u->val;
        if(u->right) {ans=ans+dfs(u->right);}
        if(u->left||u->right) ans=ans+")";
        return ans;
    } 
};
